﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Utility.NodeGraphs;

namespace Utility.BehaviourTree
{
	public class BTVisualizerClientWindow : NodeGraphEditorWindow
	{
		BTRootNode targetNode;

		[MenuItem ("Utility/BT Visualizer")]
		public static void Open ()
		{
			var window = GetWindow<BTVisualizerClientWindow> ();
			window.Show ();
		}

		public static void StopMonitor ()
		{
			var window = GetWindow<BTVisualizerClientWindow> ();
			DestroyImmediate (window.NodeGraph);
			window.NodeGraph = null;
			window.targetNode = null;
		}

		private void Update ()
		{
			if (EditorApplication.isPlaying)
			{
				if (targetNode != BTVisualizerClient.target)
				{
					Debug.Log ("Start visualize " + BTVisualizerClient.target.Name);

					targetNode = BTVisualizerClient.target;
					NodeGraph = VisualizerNodeGraph.Build (targetNode);
					(NodeGraphEditor.GetEditor (NodeGraph) as TreeNodeGraphEditor).FormatNodeGraph (NodeGraph as TreeNodeGraph);
				}

				Repaint ();
			}
		}

		private void OnEnable ()
		{

		}
	}
}